/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.swak.monitor.dubbo.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.swak.annotation.Delete;
import com.swak.annotation.Get;
import com.swak.annotation.Post;
import com.swak.annotation.Put;
import com.swak.annotation.RequestMapping;
import com.swak.annotation.RequestMethod;
import com.swak.annotation.RestApi;
import com.swak.monitor.dubbo.exception.ParamValidationException;
import com.swak.monitor.dubbo.exception.ResourceNotFoundException;
import com.swak.monitor.dubbo.model.dto.BalancingDTO;
import com.swak.monitor.dubbo.service.OverrideService;
import com.swak.monitor.dubbo.utils.Constants;
import com.swak.utils.StringUtils;

@RestApi(value = "DubboLoadBalanceApi", path = "/api/monitor/:env/rules/balancing")
public class LoadBalanceApi {

	@Autowired
	private OverrideService overrideService;
	// @Autowired
	// private ProviderService providerService;

	@Post
	public boolean createLoadbalance(BalancingDTO balancingDTO, String env) throws ParamValidationException {
		if (StringUtils.isBlank(balancingDTO.getService()) && StringUtils.isBlank(balancingDTO.getApplication())) {
			throw new ParamValidationException("Either Service or application is required.");
		}
//        String application = balancingDTO.getApplication();
//        if (StringUtils.isNotEmpty(application)
//            && this.providerService.findVersionInApplication(application).equals("2.6")) {
//            throw new VersionValidationException("dubbo 2.6 does not support application scope load balancing config");
//        }
		overrideService.saveBalance(balancingDTO);
		return true;
	}

	@Put("/:id")
	public boolean updateLoadbalance(String id, BalancingDTO balancingDTO, String env) throws ParamValidationException {
		if (id == null) {
			throw new ParamValidationException("Unknown ID!");
		}
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		BalancingDTO balancing = overrideService.findBalance(id);
		if (balancing == null) {
			throw new ResourceNotFoundException("Unknown ID!");
		}

		overrideService.saveBalance(balancingDTO);
		return true;
	}

	@RequestMapping(method = RequestMethod.GET)
	@Get
	public List<BalancingDTO> searchLoadbalances(String service, String application, String env) {

		if (StringUtils.isBlank(service) && StringUtils.isBlank(application)) {
			throw new ParamValidationException("Either service or application is required");
		}
		BalancingDTO balancingDTO;
		if (StringUtils.isNotBlank(application)) {
			balancingDTO = overrideService.findBalance(application);
		} else {
			balancingDTO = overrideService.findBalance(service);
		}
		List<BalancingDTO> balancingDTOS = new ArrayList<>();
		if (balancingDTO != null) {
			balancingDTOS.add(balancingDTO);
		}
		return balancingDTOS;
	}

	@Get("/:id")
	public BalancingDTO detailLoadBalance(String id, String env) throws ParamValidationException {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		BalancingDTO balancingDTO = overrideService.findBalance(id);
		if (balancingDTO == null) {
			throw new ResourceNotFoundException("Unknown ID!");
		}
		return balancingDTO;
	}

	@Delete("/:id")
	public boolean deleteLoadBalance(String id, String env) {
		if (id == null) {
			throw new IllegalArgumentException("Argument of id is null!");
		}
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		overrideService.deleteBalance(id);
		return true;
	}

}
